var brandList, pbcList;

$(function () {

    $("#search_reset").click(function(){
  		 $("input").val("");
         $("select").val("");
		 
    });

    //生成二维码
    $("[name='btn_new_save_qrcode']").click(function () {
        var form = window.form_new;
        var qcname = form.qcname.value;
        var pbcId = form.pbcId.value;
        var nubs = form.nubs.value;
        if (!qcname) {
            bootbox.balert("二维码批次输入有误！");
            return;
        }
        if (!pbcId) {
            bootbox.balert("请选择规格！");
            return;
        }
        if (isNaN(nubs)) {
            bootbox.balert("数量输入有误！");
            return;
        }
        if(nubs>100000){
       	 bootbox.balert("注意：一次最多生成10万箱码！");
            return;
        }
    	jQuery.ajax({
            type:'post',
            url:'/qrcodegroup/add',
            data:{'qcname':qcname,'pbcId':pbcId,'nubs':nubs},
            dataType:'json',
            cache:false,
            success:function(data){
            	var r = data.data;
            	if(r==-1){
            		tipper.danger("操作失败:二维码批次重复");
            	}else{
            		tipper.success("操作成功！");
            		window.location.href=window.location.href; 
            		window.location.reload; 
            	}
            },
            error:function (data, status) {
                // tipper.danger(data.responseJSON.msg);
                bootbox.alert({
                    message: data.responseJSON.msg,
                    size: "small",
                    title: "<i class='fa fa-info-circle text-yellow'></i> 提示"
                });
            }
       });
    	form.nubs.value = "";
    });
    
    
    $.get("/pbc?state=1", function (re) {
        var pbcList = re.data;
        var form = window.form_new_product;
        var pbcOptions = "";
        for (var i in pbcList) {
            var brand = pbcList[i];
            pbcOptions += "<option value='" + brand.pcount + "'>1*"+ brand.pcount + "</option>";
        }
        $(window.form_new.pbcId).append(pbcOptions);
        
    });
    
    
    $("#_pbcid_two,#_nbsid").on("change",function(){
    	
    	var tmpn = parseInt($("#_pbcid_two").val())*parseInt($("#_nbsid").val());
    	
    	if(!isNaN(tmpn)){
    		$("#_autonbsid").val(tmpn);
    	}
    	
    })
    
    //下载
    $("[name='btn_down']").click(function () {
        var id = $(this).data("id");
        var qcname = $(this).data("qcname");
        //console.log(qcname);
        bootbox.bconfirm("确定是否下载？", function (re) {
            if (re) {
            	 var url="/qrcodegroup/download?id="+id+"&qcname="+qcname;
                 window.open(url);
                 window.location.href=window.location.href; 
         		 window.location.reload;
            }
        });
    });

    //下载图片
    $(".btn_down_image").click(function () {
        var id = $(this).data("id");
        var qcname = $(this).data("qcname");
        //console.log(qcname);
        bootbox.bconfirm("确定是否下载？", function (re) {
            if (re) {
                var url="/qrcodegroup/qrcodeImage?id="+id+"&qcname="+qcname;
                window.open(url);
            }
        });
    });

    //数据导出
    $("[name='btn_qrcodegroup_export']").click(function () {
    	var qcname = $("[name='qcname']").val();
    	var pbcId = $("[name='pbcId']").val();//箱规
    	var isprint = $("[name='is_print']").val();
    	var tsno = $("[name='tsno']").val();//总数量 
        var teno = $("[name='teno']").val();
        var ysno = $("[name='ysno']").val();//已扫量
        var yeno = $("[name='yeno']").val();
        var xsno = $("[name='xsno']").val();//箱码数量
        var xeno = $("[name='xeno']").val();
        var psno = $("[name='psno']").val();//瓶码/盒码数量
        var peno = $("[name='peno']").val();
    	
        //有数据时才能导出
        if($("#resultTotal_hidden").val() != 0){
        	bootbox.bconfirm("确定是否导出？", function (re) {
                if (re) {
                	 var url="/qrcodegroup/export?qcname="+qcname+"&=pbcId"+pbcId+"&isprint="+isprint+"&tsno="+tsno
                	 +"&teno="+teno+"&ysno="+ysno+"&yeno="+yeno+"&xsno="+xsno+"&xeno"+xeno+"&psno="+psno+"&peno="+peno;
                     window.open(url);
                }
            });
        }else{
        	bootbox.balert("没有数据，无法导出!");
        }
    });
  
});
